<template>
<view :style="theme" class="page">
	<!-- 头部图标 -->
	<u-sticky z-index="1000" :offset-top="gaodu"  :bgColor="fenggeyanse =='light'? '#fff4f4':'#242424'">
		<!-- #ifdef APP-PLUS -->
		<u-gap height="30" bgColor="#ffffff"></u-gap>
		<!-- #endif -->
		<view class="d-kuang-10">
			<view class="d-beijing-bai ">
				<view class="d-yuanjiao-10 d-neikuang-5 ">
					<u-row
						customStyle="margin-bottom: 10px"
						gutter="10"
					>
						<u-col span="4">
							<view class="demo-layout bg-purple d-neirong-juzhong d-box" @click="pinglun(10)">
								<image mode="aspectFill" src="/static/images/huifu.png" class="d-yuanjiao-10 d-img-40 d-shang-10" ></image>
								<view class="box-you">
									 <u-badge :type="type" max="99" :value="tongji.pinglunshu" bgColor="#e3310d"></u-badge>
								</view>
								<view class="d-wenzi-yanse-hui d-ziti-12">评论</view>
							</view>
						</u-col>
						<u-col span="4">
							<view class="demo-layout bg-purple d-neirong-juzhong d-box" @click="tongzhi()">
								
								<image mode="aspectFill" src="/static/images/duanxin.png" class="d-yuanjiao-10 d-img-40 d-shang-10" ></image>
								<view class="box-you">
									 <u-badge :type="type" max="99" :value="tongji.tongzhishu" bgColor="#e3310d"></u-badge>
								</view>
								<view class="d-wenzi-yanse-hui d-ziti-12">通知</view>
							</view>
						</u-col>
						<u-col span="4">
							<view class="demo-layout bg-purple d-neirong-juzhong d-box" @click="dianzan(4)">
	
								<image mode="aspectFill" src="/static/images/dianzan.png" class="d-yuanjiao-10 d-img-40 d-shang-10" ></image>
								<view class="box-you">
									 <u-badge :type="type" max="99" :value="tongji.dianzanshu" bgColor="#e3310d"></u-badge>
								</view>
								<view class="d-wenzi-yanse-hui d-ziti-12">点赞</view>
							</view>
						</u-col>
					</u-row>
				</view>
			</view>
		</view>
	</u-sticky>
	<!-- 短信内容 -->
	<view >
		
	</view>
	<view class="d-clear"></view>
</view>
</template>

<script>

import config from "../../utils/config";
var Api = require("../../utils/api.js");
var util = require("../../utils/util.js");

var wxApi = require("../../utils/wxApi.js");
var wxRequest = require("../../utils/wxRequest.js");
var webSiteName = config.getWebsiteName;
var domain = config.getDomain;
var wss = config.getWss;
export default {
  data() {
    return {
      text: "消息",
      categoriesList: {},
      floatDisplay: "none",
      userInfo: {},
      webSiteName: webSiteName,
      domain: domain,
	  www: [],
	  quanlist: [],
	  type:"success",
	  dianzanshu:5,
	  tongzhishu:5,
	  pinglunshu:5,
	  tongji:{},
	  uid:'',
	  gaodu:'',
	  fenggeyanse:'',
    };
  },

  components: {

  },
  props: {},
  onLoad: function (options) {
	var duixin = this;
    this.getWww();
    duixin.getGaodu();
	//判断用户是否登录
	var userInfo = uni.getStorageSync('userInfo');
	
	if (userInfo) {
		this.showuid = true;
		var member = uni.getStorageSync('member');
		var memberuid = uni.getStorageSync('memberuid');
		this.uid = memberuid;
		//console.log("member1",member);
		//console.log("userInfo1",userInfo);
		// 保留二次开发
		this.getIm();
		this.getTongjis(memberuid);
		this.getimin(memberuid,member);

	} else {
		//否则跳转到登录页面
		uni.reLaunch({ url: '/pages/login/login'}) // 跳转到首页
	}


  },
  onBackPress() {
      // 刷新数据的逻辑 
	   console.log('ob');
      this.getTongjis(this.uid);
      return true; // 返回true表示拦截返回事件，继续执行后续操作  
    }, 
	
	onUnload() {
		console.log('ou');
	  this.getTongjis(this.uid);
	},
  onShow: function () {
	  var duixin = this;
	  uni.setNavigationBarTitle({
	    title: '消息'
	  });
	  
	  //判断用户是否登录
	  var userInfo = uni.getStorageSync('userInfo');
	  var member = uni.getStorageSync('member');
	  if (userInfo) {
		  this.getTongjis(this.uid);
		  this.getimin(this.uid,member);
	  }
	  // 暗黑模式
	  var initialThemes = uni.getStorageSync('theme');
	  if(!initialThemes){
	  	duixin.fenggeyanse = 'light';
	  	uni.setStorageSync('theme', 'light');
	  } else {
	  	duixin.fenggeyanse = initialThemes;
	  }
	  uni.setNavigationBarColor({
	  	backgroundColor: this.fenggeyanse =='light'? '#ffffff':'#151515',
	  	frontColor: this.fenggeyanse =='light'? '#000000':'#ffffff',
	  })
  },
  onShareAppMessage: function () {
    return {
      title: '分享“' + config.getWebsiteName + '”消息.',
      path: 'pages/quan/wode',
      success: function (res) {// 转发成功
      },
      fail: function (res) {// 转发失败
      }
    };
  },
  
  methods: {
	  getGaodu(){
		  var duixin = this;

		  // #ifdef H5 
                 duixin.gaodu='-44';
		  // #endif
		  // #ifdef APP-PLUS
		   duixin.gaodu='0';
		  // #endif
		  // #ifdef MP
		        duixin.gaodu='0';
		  // #endif
	  },
	//IM 链接
	getIm(){ 
		uni.onSocketMessage(function (res) {
		  console.log('收到消息：', res.data);
		}); 
		
	},
	//获取站点
	getWww() {
	  	var self = this;
	  	self.setData({
	  	  www: []
	  	});
	  	var getTubiaoRequest = wxRequest.getRequest(Api.getWwwUrl());
	  	getTubiaoRequest.then(response => {
	  		if (response.data.code == 1) {
	  		  //console.log(response.data.data); 
	  		  self.www = response.data.data
	  		} else {
	  		  self.setData({
	  			displaySwiper: "none"
	  		  });
	  		}
	  	});
	},
	//获取消息统计
	getTongjis(uid) {
	  	var self = this;
	  	//console.log('xx'); 
	  	var getXiaoxitongjiRequest = wxRequest.getRequest(Api.getXiaoxitongji(uid));
	  	getXiaoxitongjiRequest.then(response => {
	  		if (response.data.code == 1) {
	  		  //console.log(response.data.data); 
	  		  self.tongji = response.data.data
			  self.tongzhis(response.data.data);
	  		} else {
	  		  self.setData({
	  			displaySwiper: "none"
	  		  });
	  		}
	  	});
	},
	// 通知
	tongzhis(e){
		var tongjishu = e;
		console.log('通知',e.zongshu);
		if(tongjishu.zongshu >0){
			setTimeout(() => {
				uni.setTabBarBadge({ // 显示红点
					index: 3, // tabbar下标
					text: tongjishu.zongshu.toString() // 数字
				})
			 });
		} else {
			setTimeout(() => {
				uni.removeTabBarBadge({ // 隐藏红点
					index: 3, // tabbar下标
				})
			 });
		}
	},

	// 点击
	quanzi(tieid) {
		console.log('点击',tieid); 
		var url = '../quan/quanzi?&id='+tieid;
		uni.navigateTo({
			url: url
		});
	},
	// 点赞
	dianzan(id) {
		var url = '../xiaoxiye/dianzan?&id='+id;
		console.log('url',url)
		uni.navigateTo({
			url: url
		});
	},
	// 通知
	tongzhi() {
		var url = '../xiaoxiye/tongzhi';
		uni.navigateTo({
			url: url
		});
	},
	// 评论
	pinglun(id) {
		var url = '../xiaoxiye/pinglun?&id='+id;
		uni.navigateTo({
			url: url
		});
	},
	//重新链接
	getimin(uid,member){
		var duixin = this;
		if (!duixin.duixinimin || duixin.duixinimin.readyState == 3) { 
			/*#ifdef H5*/
			duixin.duixinimin = new WebSocket(wss);  
			
			duixin.duixinimin.onopen = () => {  
				console.log('im连接成功',duixin.duixinim);  
				duixin.message = '已连接';
				var login_data = '{"type":"login","name":"'+member.name+'","uid":"'+uid+'"}';
				console.log("登录成功，发送登录数据:"+login_data);
				duixin.duixinim.send(login_data);
			};
			duixin.duixinimin.onmessage = duixin.getOnmessage;  
			  
			duixin.duixinimin.onclose = function(event) {
			  console.log('im已关闭，尝试重新连接...', event);
			  duixin.message = '连接已断开';  
			  setTimeout(() => {  
				duixin.getimin(); // 延迟重新连接，避免频繁尝试连接
				console.log('im连接成功in'); 
			  }); // 5秒后重新连接  , 5000
			};  
			duixin.duixinimin.onerror = function(error) {  
			  console.error('im错误:', error);  
			}; 
			/*#endif*/
			/*#ifdef MP-WEIXIN  || APP-PLUS*/
			uni.connectSocket({
			  url: wss,
			  success(data) {
			     console.log("websocket连接成功",data);
			  },
			  fail(err) {
				console.error('WebSocket连接失败', err);
			  }
			});
			uni.onSocketOpen(function (res) {
			  console.log('WebSocket连接已打开！');
			  var login_data = '{"type":"login","name":"'+member.name+'","uid":"'+uid+'"}';
			    uni.sendSocketMessage({
			        data: login_data
			    });
			});
			uni.onSocketMessage(function (res) {
			  console.log('收到服务器内容：' + res.data);
			  duixin.getOnmessage(res);
			});
			uni.onSocketClose(function (res) {
			  console.log('WebSocket 已关闭！');
			  setTimeout(() => {
				duixin.getimin(); // 延迟重新连接，避免频繁尝试连接
				console.log('im连接成功in'); 
			  }); // 5秒后重新连接  , 5000
			});
			uni.onSocketError(function (res) {
			  console.log('WebSocket连接打开失败，请检查！');
			});
			/*#endif*/
		} else {  
		console.log('im连接已存在');  
		} 
	},
	getOnmessage(e){
		var duixin = this;
		//console.log('收信息',e.data);
		var shouxin = JSON.parse(e.data); 
		duixin.shouxin = shouxin;
		if(shouxin.type == 'zan') {
			if(duixin.uid==shouxin.tieuid){
				duixin.toptishishow = true;
				duixin.dianzanname = shouxin.tozanname;
				duixin.dianzantouxiang = shouxin.avatar;
				duixin.duanxintongzhi = 1;
				duixin.getTongjis(duixin.uid);
				setTimeout(() => {  
					duixin.toptishishow = false; // 五秒后隐藏
				}, 3000); // 延迟时间为 5000 毫秒（即 5 秒）
			}
			
		}
	},
  }
};
</script>
<style lang="scss">

/*page {
	background-color: var(--nav-color-bg);
}*/
</style>